<template>
    <div class="app-container app-containerMoney">
        <div class="filter-container">
            <region-selector style="width:168px;" :hideProvince="true" :hideCity="true" :hideStreet="true" :hideCommunity="true" :province="listQuery.region.province" :city="listQuery.region.city" :area="listQuery.region.area" @region="setListQueryAreaCode">
            </region-selector>
            <el-select style="marginRight:15px;" size="small" v-model="listQuery.approvalKey" placeholder="请选择业务类型">
              <el-option
                v-for="item in options['approvalLessKey']"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                </el-option>
            </el-select>
            <el-date-picker size="small"
							v-model="listQuery.year"
							type="year" value-format="yyyy"
							placeholder="选择年">
						</el-date-picker>
            <el-button size="small" class="filter-item filter-itemBut" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
        </div>
        <el-row class="filter-container-option">
            <el-col :span="1">
                <el-button type="warning" size="small" @click="handleGrant" plain>拨款</el-button>
            </el-col>
        </el-row>
        <el-table :data="list" v-loading.body="listLoading" element-loading-text="Loading" border fit
            highlight-current-row>
            <el-table-column label="年份" align="center">
                <template slot-scope="scope">
                    {{scope.row.year}}
                </template>
            </el-table-column>
            <el-table-column label="县区" align="center">
                <template slot-scope="scope">
                    {{scope.row.areaName}}
                </template>
            </el-table-column>
            <el-table-column label="业务" align="center">
                <template slot-scope="scope">
                    {{scope.row.approvalKey | mapLabel('approvalKey')}}
                </template>
            </el-table-column>
            <el-table-column label="总拨款" align="center">
                <template slot-scope="scope">
                    {{scope.row.money}}
                </template>
            </el-table-column>
            <el-table-column label="已使用" align="center">
                <template slot-scope="scope">
                    {{scope.row.used}}
                </template>
            </el-table-column>
            <el-table-column label="剩余资金" align="center">
                <template slot-scope="scope">
                    {{scope.row.over}}
                </template>
            </el-table-column>
            <!-- <el-table-column label="拨款时间" align="center">
                <template slot-scope="scope">
                    123
                </template>
            </el-table-column>
            <el-table-column label="拨款说明" align="center">
                <template slot-scope="scope">
                    123
                </template>
            </el-table-column> -->
        </el-table>
        <div class="pagination-container">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.size" layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
    <el-dialog title="拨款" :visible.sync="dialogFormVisible">
      <el-form size="small" :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="80px" style='width: calc(100% - 100px); margin-left:50px;; margin-right:50px;'>
        <el-form-item label="地区" prop="areaCode" class="dialog-areaCode_User">
          <region-selector :hideProvince="true" :hideCity="true" :hideStreet="true" :hideCommunity="true" :province="region.province" :city="region.city" :area="region.area" 
            :street="region.street"  :community="region.community"
             @region="setTempAreaCode" >
             </region-selector>
            <el-input style="display:none" type="hidden" v-model="temp.areaCode"></el-input>
        </el-form-item>
				<el-form-item label="业务类型" prop="approvalKey">
          <el-select style="marginRight:15px;" size="small" v-model="temp.approvalKey" placeholder="请选择业务类型">
              <el-option
                v-for="item in options['approvalLessKey']"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
				<el-form-item label="拨款金额" prop="money">
          <el-input v-model="temp.money"></el-input>
        </el-form-item>
				<el-form-item label="拨款说明" prop="remark">
          <el-input type="textarea" v-model="temp.remark"></el-input>
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" size="small" @click="grant" plain>确认</el-button>
      </div>
    </el-dialog>
    </div>
</template>
<script>
import { queryYearPage, create } from "@/api/money";
import RegionSelector from "@/components/RegionSelector";
export default {
  components: {
    RegionSelector
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      dialogFormVisible: false,
      listQuery: {
        page: 1,
        size: 10,
        areaCode: null,
        approvalKey: null,
        year: null,
        sort: "+id",
        region: {
          province: "陕西省",
          city: "西安市",
          area: 0,
          street: 0,
          community: 0
        }
      },
      options: this.$store.state.dict.options,
      region: {
        province: "陕西省",
        city: "西安市",
        area: 0,
        street: 0,
        community: 0
      },
      temp: {
        approvalKey: null,
        money: null,
        areaCode: null,
        remark: null
      },
      rules: {
        approvalKey: [
          { required: true, message: "请选择业务", trigger: "change" }
        ],
        areaCode: [
          { required: true, message: "请选择地区", trigger: "change" }
        ],
        money: [{ required: true, message: "请输入拨款金额", trigger: "blur" }]
      }
    };
  },
  mounted: function() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      queryYearPage(this.listQuery).then(response => {
        this.list = response.data.records;
        this.total = response.data.total;
        this.listLoading = false;
      });
      this.listLoading = false;
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.listQuery.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.fetchData();
    },
    setListQueryAreaCode(value) {
      if (value.code) {
        this.listQuery.areaCode = value.code;
      }
    },
    setTempAreaCode(value) {
      if (value.code) {
        this.temp.areaCode = value.code
      }
    },
    handleGrant() {
      this.resetTemp();
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    grant() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          create(this.temp).then(() => {
            this.fetchData();
            this.dialogFormVisible = false;
            this.$notify({
              title: "成功",
              message: "操作成功",
              type: "success",
              duration: 2000
            });
          });
        }
      });
    },
    resetTemp() {
      this.temp = {
        approvalKey: null,
        money: null,
        areaCode: null,
        remark: null
      };
    }
  }
};
</script>

<style scoped>
.filter-container {
  margin-bottom: 20px;
  overflow: hidden;
  display: flex;
  align-items: center;
}

.filter-container-option {
  display: flex;
  flex-wrap: nowrap;
  margin-bottom: 15px;
}
.pagination-container {
  margin-top: 25px;
  overflow: hidden;
  display: flex;
  justify-content: flex-end;
}

.app-containerMoney {
  margin-right: 20px;
}

.filter-itemBut {
  margin-left: 25px;
}
</style>